package org.csu.forum.interceptor;

import org.csu.forum.exceptioin.TokenException;
import org.csu.forum.utils.TokenUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        System.out.println(request.getRequestURI());
        if(request.getMethod().equals("post")&&request.getRequestURI().equals("/users"))
        {
            return true;
        }
        if ("isExist".equals(request.getParameter("action")))//如果是查看用户名是否存在就无需过滤
            return true;
        String token = request.getHeader("token");
        if (token == null || !TokenUtils.verify(token)) {//如果token为空或者验证不通过
            throw new TokenException(10001, "token不可用！");
        }
        return true;
    }
}
